home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpsehttp / news / postnews < prev    next >
Encoding:
Text File  |  1995-05-16  |  2.8 KB  |  109 lines

  1. #!/usr/local/bin/perl 
  2. #/****************************************************
  3. #**
  4. #** SOURCE NAME | postnews (Post News)
  5. #**             | 
  6. #**    SYNOPSIS | postnews [-h hostname] [-p port] [-W timeout]
  7. #**             | 
  8. #** DESCRIPTION | postnews goes to a specified NNTP server
  9. #**             | and posts the article from standard input
  10. #**             | Server can be specified (in order of precedence):
  11. #**             | * in -h <host> option
  12. #**             | * in NNTPSERVER environment variable
  13. #**             | * output of command 'domainname' by default
  14. #**             | 
  15. #**     CHANGES | Programmer:         Date:     Reason/Comments
  16. #**        | Pavel Klark         05-06-94  VERSION 1.0 (postnews)
  17. #**             | 
  18. #****************************************************/
  19.  
  20. require 'sys/socket.ph'; # The way I coded the sockets is this necessary?
  21. require 'getopts.pl';
  22. # Valid options are:
  23. # -p portnumber : Port to connect to; default 119
  24. # -h host : Server host to connect to
  25. # -W timeout : Timeout wait period for response, sec.; default 900 (= 15min)
  26. $opt_h = $ENV{'NNTPSERVER'};
  27. $domain = `domainname`;
  28. chop $domain;
  29. $opt_h = substr($domain,1) unless $opt_h;
  30. $opt_p = 119;
  31. $opt_W = 900;
  32. &Getopts ('h:p:W:');
  33.  
  34. $VERSION = '2.0';
  35.  
  36. $port = $opt_p; # For NNTP
  37. # HOSTNAME for the server...
  38. $host = $opt_h;
  39. # Pack format...
  40. $sockaddr = 'S n a4 x8';
  41.  
  42. $waittime = $opt_W;
  43.  
  44. $DOMAIN = &AF_INET;
  45. $STYLE = &SOCK_STREAM;
  46.  
  47. $rin = $rout = '';
  48.  
  49. ($name, $aliases, $proto) = getprotobyname('tcp');
  50. ($name, $aliases, $type, $len, $hostaddr) = gethostbyname($host);
  51.  
  52. $sock = pack($sockaddr, $DOMAIN, $port, $hostaddr);
  53.  
  54. print "Connecting to $host...";
  55. $| = 1;
  56. socket(S, $DOMAIN, $STYLE, $proto) || die $!;
  57. connect(S, $sock) || die $!;
  58. select(S); $| = 1; select(STDOUT);
  59. #set up for select
  60. vec($rin, fileno(S), 1) = 1;
  61. #this select will block until the server gives us something.
  62. $nfound = select($rout=$rin, undef, undef, $waittime);
  63. if ($nfound == 0)
  64. {
  65.     print "Socket timed out...\n";
  66.     exit 1;
  67. }
  68. $_ = <S>; #Read one line to see if we got a good connection.
  69. if ($_ !~ /^2../)
  70. {
  71.     print "\n";
  72.     print;
  73.     die "News server '$host' unavailable";
  74. }
  75. print "ok\n";
  76. print(S "post\n");
  77. #this select will block until the server gives us something.
  78. $nfound = select($rout=$rin, undef, undef, $waittime);
  79. if ($nfound == 0)
  80. {
  81.     print "Socket timed out...\n";
  82.     exit 1;
  83. }
  84. $_ = <S>; #Make sure the posting command worked...
  85. ($stat, $num, $first, $last) = split;
  86. if( $stat !~ /^3../ )
  87. {
  88.     print;
  89. }
  90. print "Posting article...\n";
  91. while (<STDIN>) {
  92.     print;
  93.     s/^\./../;
  94.     print(S $_);
  95. }
  96. print(S ".\n");
  97. select(S);$| = 1;select(STDOUT);
  98. #this select will block until the server gives us something.
  99. $nfound = select($rout=$rin, undef, undef, $waittime);
  100. if ($nfound == 0)
  101. {
  102.     print "Socket timed out...";
  103.     exit 1;
  104. }
  105. $_ = <S>;
  106. print;
  107. print(S "quit\n");
  108. close(S);
  109.